package pixeleditor.gui;

import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;
import common.i18n.Language;
import common.util.FileHandler;

import pixeleditor.ctrl.ControlFactory;


@SuppressWarnings("serial")
public class Menu extends JMenuBar implements IChangeLanguage {
	private JMenu mnuFile = new JMenu();
	private JMenu mnuHelp = new JMenu();
	private JMenuItem mnuFileNewMap = new JMenuItem();
	private JMenuItem mnuFileLoadMap = new JMenuItem();
	private JMenuItem mnuFileSaveMap = new JMenuItem();
	private JMenuItem mnuFileLanguage = new JMenu();
	private JMenuItem[] mnuFileLanguageLanguages;
	private JMenuItem mnuFileExit = new JMenuItem();
	private JMenuItem mnuHelpAbout = new JMenuItem();
	private JMenuItem mnuHelpHelp = new JMenuItem();
	
	public static final int NUMBER_OF_LANGUAGES;
	static {
		NUMBER_OF_LANGUAGES = FileHandler.getNumberOfLanguages();
	}
	
	public Menu() {
		ChangeLanguage.addObserver(this);
		initComponents();
		initControls();
		setLanguage(ChangeLanguage.currentLanguage);
	}
	
	private void initControls() {
		Action a = (Action) ControlFactory.getControl(ControlFactory.MENU_CTRL);
		mnuFileNewMap.setAction(a);
		mnuFileLoadMap.setAction(a);
		mnuFileSaveMap.setAction(a);
		mnuFileExit.setAction(a);
		mnuHelpAbout.setAction(a);
		mnuHelpHelp.setAction(a);
		
		a.putValue("Menu.inewMap", mnuFileNewMap);
		a.putValue("Menu.iloadMap", mnuFileLoadMap);
		a.putValue("Menu.isaveMap", mnuFileSaveMap);
		a.putValue("Menu.iexit", mnuFileExit);
		a.putValue("Menu.iabout", mnuHelpAbout);
		a.putValue("Menu.ihelp", mnuHelpHelp);
		
		
		for(int i=0; i<NUMBER_OF_LANGUAGES; i++) {
			mnuFileLanguageLanguages[i].setAction(a);
			a.putValue("Menu.ilanguage" + (i+1), mnuFileLanguageLanguages[i]);
    	}
	}
	
	private void initComponents() {
		add(mnuFile);
		mnuFile.add(mnuFileNewMap);
		mnuFile.add(mnuFileLoadMap);
		mnuFile.add(mnuFileSaveMap);
		mnuFile.add(mnuFileLanguage);
		
		mnuFileLanguageLanguages = new JMenuItem[NUMBER_OF_LANGUAGES];
		for(int i=0; i<NUMBER_OF_LANGUAGES; i++) {
			mnuFileLanguageLanguages[i] = new JMenuItem();
			mnuFileLanguage.add(mnuFileLanguageLanguages[i]);
    	}
		mnuFile.add(mnuFileExit);
		add(mnuHelp);
		mnuHelp.add(mnuHelpAbout);
		mnuHelp.add(mnuHelpHelp);
	}

	@Override
	public void setLanguage(ResourceBundle rb) {
		mnuFile.setText(rb.getString("mnuFile"));
		mnuHelp.setText(rb.getString("mnuHelp"));
		mnuFileNewMap.setText(rb.getString("mnuFileNewMap"));
		mnuFileLoadMap.setText(rb.getString("mnuFileLoadMap"));
		mnuFileSaveMap.setText(rb.getString("mnuFileSaveMap"));
		mnuFileLanguage.setText(rb.getString("mnuFileLanguage"));
		
    	for(int i=0; i<NUMBER_OF_LANGUAGES; i++) {
    		mnuFileLanguageLanguages[i].setText(Language.lang.get(i));
    	}
		
		mnuFileExit.setText(rb.getString("mnuFileExit"));
		mnuHelpAbout.setText(rb.getString("mnuHelpAbout"));
		mnuHelpHelp.setText(rb.getString("mnuHelpHelp"));
	}
}
